package com.hpjpw.ido.controller;

import java.util.List;

import com.hpjpw.idoapp.service.ReceiveMessageListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.hpjpw.ido.domain.Message;
import com.hpjpw.ido.service.IMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统消息Controller
 * 
 * @author hpj
 * @date 2021-01-02
 */
@Api(value ="MessageController")
@RestController
@RequestMapping("/ido/message")
public class MessageController extends BaseController
{
    @Autowired
    private IMessageService messageService;

    @Autowired
    private ReceiveMessageListService receiveMessageListService;

    /**
     * 查询系统消息列表
     */
    @PreAuthorize("@ss.hasPermi('ido:message:list')")
    @GetMapping("/list")
    @ApiOperation("查询系统消息列表")
    public TableDataInfo list(Message message)
    {
        startPage();
        List<Message> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }

    /**
     * 导出系统消息列表
     */
    @PreAuthorize("@ss.hasPermi('ido:message:export')")
    @Log(title = "系统消息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出系统消息列表")
    public AjaxResult export(Message message)
    {
        List<Message> list = messageService.selectMessageList(message);
        ExcelUtil<Message> util = new ExcelUtil<Message>(Message.class);
        return util.exportExcel(list, "message");
    }

    /**
     * 获取系统消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ido:message:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取系统消息详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(messageService.selectMessageById(id));
    }

    /**
     * 新增系统消息
     */
    @PreAuthorize("@ss.hasPermi('ido:message:add')")
    @Log(title = "系统消息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增系统消息")
    public AjaxResult add(@RequestBody Message message)
    {
        return toAjax(messageService.insertMessage(message));
    }

    /**
     * 修改系统消息
     */
    @PreAuthorize("@ss.hasPermi('ido:message:edit')")
    @Log(title = "系统消息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改系统消息")
    public AjaxResult edit(@RequestBody Message message)
    {
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 删除系统消息
     */
    @PreAuthorize("@ss.hasPermi('ido:message:remove')")
    @Log(title = "系统消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除系统消息")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        //级联删除系统消息发送记录
        for (Long id : ids) {
            receiveMessageListService.deleteMessageReceiveByMessageId(id);
        }
        return toAjax(messageService.deleteMessageByIds(ids));
    }
}
